using System.Collections;
using System.Collections.Generic;
using Awaiter;
using Canvas.ImageRotate;
using Nsb.Classes;
using YesPlayLove.Classes;
using YesPlayLove.Clusters;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace YoshiFlowMain
{
    public class SearchPage : YoshiPage
    {
        public SearchPage()
        {
            BasicBuildAw(Await.AsyncAw, SearchTheme.PageTitle);
        }

        public override void RefreshAw(Await awp)
        {
            jQueryObject title = jQuery.FromHtml("<h2/>").Html("Search for a partner !!! :-)").CSS("margin", "50px").AppendTo(PageCluster);

            new Await()
                .AddAw(ProfileData.SearchForUsersAw, new Dictionary( /* search attributes go here */))
                .AddDx(
                    delegate(Await aw)
                    {
                        List<string> ids = (List<string>)aw[PkgBase.ResultRx];
                        jQueryObject grid = jQuery.FromHtml("<ul class='SearchResult'/>")
                            .InsertBefore(title)
                            ;
                        int cnt = 0;
                        foreach (string id in ids)
                        {
                            if (id == Master.Profile.MyId)
                                continue;
                            if (!Master.Testing && ThemeBase.TestUsers.ContainsKey(id))
                                continue;
                            new Await()
                                .AddAw(ProfileData.GetUserByIdAw, id)
                                .AddDx(
                                    delegate(Await aw2)
                                    {
                                        int deg = ((++cnt) % 2) == 0 ? 365 : -365;
                                        YoshiUser usr = (YoshiUser)aw2[PkgBase.ResultRx];
                                        SearchResult(usr).AppendTo(grid).Plugin<IRot>().Rotate(new Dictionary("animateTo", deg, "duration", 800));
                                    }
                                )
                                .Commit();
                        }
                    }
                )
                .Commit();


            new Await()
                .AddAw(SearchCluster.Aw, title)
                .Commit(awp);
        }

        public jQueryObject SearchResult(YoshiUser usr)
        {
            jQueryObject item = jQuery.FromHtml("<li/>");

            jQueryObject photo = jQuery.FromHtml("<div class='Photo'/>")
                .AppendTo(item)
                ;

            jQuery.FromHtml("<img/>")
                .AppendTo(photo)
                .Attribute("src", usr.PhotoUrl)
                ;

            jQuery.FromHtml("<span class='Name'/>")
                .AppendTo(item)
                .Html(usr.alias)
                ;

            jQuery.FromHtml("<span class='MessageBtn'/>")
                .AppendTo(item)
                .Html("send message")
                .Click(delegate(jQueryEvent e) { Master.AlivenessCheck(delegate { EmailReply.SendPopupAw(Await.AsyncAw, usr, e); }); });

            return item;
        }

        public override void UpdatePage(PageUpdate pu)
        {
            if (TopBar != null)
                TopBar.UpdatePage(pu);
        }
    }
}